package entity

import (
	"errors"
	"fmt"

	//	"log"
	"time"

	"shqsoft.cn/pasnj/base"
	//log "github.com/sirupsen/logrus"
)

//UserNewOrgan 员工调动
type UserNewOrgan struct {
	ID            int          `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	EMID          int          `gorm:"column:EMID"`
	User          User         `gorm:"ForeignKey:EMID"`
	NEWEMID       *int         `gorm:"column:NEWEMID"`
	NewUser       User         `gorm:"ForeignKey:NEWEMID"`
	OUTID         *int         `gorm:"column:OUTID"`
	OOrganization Organization `gorm:"ForeignKey:OUTID"`
	NUTID         int          `gorm:"column:NUTID"`
	NOrganization Organization `gorm:"ForeignKey:NUTID"`
	YJBD          string       `gorm:"column:YJBD"`
	YJBDCaption   string       `gorm:"-"`
	DDDT          time.Time    `gorm:"column:DDDT"`
	JZDT          time.Time    `gorm:"column:JZDT"`
	STEP          int          `gorm:"column:STEP"`
	STEPCaption   string       `gorm:"-"`
	DGCKISDR      bool         `gorm:"column:DGCKISDR"`
	DGDKISDR      bool         `gorm:"column:DGDKISDR"`
	DSCKISDR      bool         `gorm:"column:DSCKISDR"`
	DSDKISDR      bool         `gorm:"column:DSDKISDR"`
}

func (UserNewOrgan) TableName() string {
	return "T_SysUser_YGDD"
}
func (u *UserNewOrgan) BeforeCreate() (err error) {
	if u.EMID == 0 {
		return errors.New("默认员工不允许调动部门！")
	}
	var date time.Time
	var count int
	row := base.DB.Raw("SELECT top 1 DDDT,COUNT(*) count FROM T_SysUser A INNER JOIN V_SYSUSER_YGDD B ON A.ID=B.EMID WHERE A.ID=? AND DDDT>=? group by DDDT order by DDDT desc", u.EMID, u.DDDT.Format("20060102")).Row()
	if err != nil {
		return
	}
	row.Scan(&date, &count)
	if count > 0 {
		return fmt.Errorf("员工调动日期必须在上次调动日期%s之后", date.Format("20060102"))
	}
	var updateUser User
	err = base.DB.Where("ID =?", u.EMID).Find(&updateUser).Error
	if err != nil {
		return err
	}
	u.OUTID = updateUser.OrganizationID
	if *u.OUTID == u.NUTID {
		return errors.New("调出部门和调入部门不能相同！")
	}
	u.JZDT, err = time.Parse("20060102", "20991231")
	if err != nil {
		return err
	}
	//	//修改员工部门
	//	updateUser.OrganizationID = &u.NUTID
	//	err = base.DB.Model(&(User{})).Save(&updateUser).Error
	//	if err != nil {
	//		return err
	//	}
	return
}

//新增后处理
func (u *UserNewOrgan) AfterCreate() (err error) {
	//	if u.YJBD == "3" {
	//		err = base.DB.Exec("exec UP_SysUser_Bmdd ?,?,?", u.EMID, u.OEMID, u.DDDT).Error
	//		if err != nil {
	//			return err
	//		}
	//	}
	return nil
}

func (u *UserNewOrgan) AfterFind() (err error) {
	if u.YJBD == "1" {
		u.YJBDCaption = "业绩全部带走(含历史)"
	} else if u.YJBD == "2" {
		u.YJBDCaption = "业绩全部留给其他员工"
	} else if u.YJBD == "3" {
		u.YJBDCaption = "业绩全部留给自己"
	} else if u.YJBD == "4" {
		u.YJBDCaption = "业绩部分带走"
	} else if u.YJBD == "5" {
		u.YJBDCaption = "业绩全部带走(不含历史)"
	}
	if u.STEP == -1 {
		u.STEPCaption = "不同意"
	} else if u.STEP == 0 {
		u.STEPCaption = "填写申请"
	} else if u.STEP == 1 {
		u.STEPCaption = "复核确认"
	} else {
		u.STEPCaption = "完成"
	}
	return
}
func UserNewOrganEF() base.EntityFunc {
	return func() interface{} {
		entity := UserNewOrgan{}
		return &entity
	}
}
func UserNewOrganESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []UserNewOrgan
		return &entities
	}
}

func init() {
	relationship := []string{"NOrganization", "User", "OOrganization", "NewUser"}
	base.DaoPoolObj.Put("UserNewOrgan", base.NewDefaultDao(UserNewOrganEF(), UserNewOrganESF(), relationship))
}
